package com.cn.unarmed.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cn.unarmed.base.BaseActivity;
import com.cn.unarmed.commonutils.AppUtils;

import java.util.ArrayList;
import java.util.List;

public class TwoListViewActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view2628);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        initData();
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        if (TitleBar.LEFT.equals(component)) {
            finish();
        }
    }

    TextView textView1, textView2;
    ListView listView1, listView2;
    LinearLayout activity_view2628;

    private void initView() {
        textView1 = (TextView) this.findViewById(R.id.text1);
        textView2 = (TextView) this.findViewById(R.id.text2);
        listView1 = (ListView) this.findViewById(R.id.list_1);
        listView2 = (ListView) this.findViewById(R.id.list_2);

        textView1.setClickable(true);
        textView2.setClickable(true);
        textView1.setOnClickListener(this);
        textView2.setOnClickListener(this);

        activity_view2628 = (LinearLayout) this.findViewById(R.id.activity_view2628);
    }

    private void initData() {
        //生成测试元数据list1
        List<String> list1 = new ArrayList<>();
        //生成测试数据元list2
        List<String> list2 = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list1.add("数据源1的数据" + i);
            list2.add("数据源2的数据" + i);
        }
        ThisAdapter adapter1 = new ThisAdapter(list1, this);
        ThisAdapter adapter2 = new ThisAdapter(list2, this);
        listView1.setAdapter(adapter1);
        listView2.setAdapter(adapter2);
        AppUtils.setListViewHeightBasedOnChildren(listView1);
        AppUtils.setListViewHeightBasedOnChildren(listView2);
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            }
        });
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.text1:
                if (listView1.getVisibility() == View.GONE) {
                    listView1.setVisibility(View.VISIBLE);
                    listView2.setVisibility(View.GONE);
                    resetListViewHeight(listView1);
                    AppUtils.setListViewHeightBasedOnChildren(listView2);
                } else {
                    listView1.setVisibility(View.GONE);
                    listView2.setVisibility(View.VISIBLE);
                    resetListViewHeight(listView2);
                    AppUtils.setListViewHeightBasedOnChildren(listView1);
                }
                break;
            case R.id.text2:
                if (listView2.getVisibility() == View.GONE) {
                    listView2.setVisibility(View.VISIBLE);
                    listView1.setVisibility(View.GONE);
                    resetListViewHeight(listView2);
                    AppUtils.setListViewHeightBasedOnChildren(listView1);
                } else {
                    listView2.setVisibility(View.GONE);
                    listView1.setVisibility(View.VISIBLE);
                    resetListViewHeight(listView1);
                    AppUtils.setListViewHeightBasedOnChildren(listView2);
                }
                break;
            default:
                break;
        }
    }

    //手动修改listView的高度
    private void resetListViewHeight(ListView listView) {
        if (activity_view2628 == null) return;
        if (listView == null) return;
        ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
        layoutParams.height = activity_view2628.getMeasuredHeight() -
                textView1.getMeasuredHeight() - textView2.getMeasuredHeight();
        listView.setLayoutParams(layoutParams);
    }

    class ThisAdapter extends BaseAdapter {

        List<String> list;
        LayoutInflater layoutInflater;

        ThisAdapter(List<String> list, Context context) {
            this.list = list;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder = null;
            if (view == null) {
                holder = new Holder();
                view = layoutInflater.inflate(R.layout.item_view0628, null);
                holder.textView = (TextView) view.findViewById(R.id.text_item);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            holder.textView.setText(list.get(i));
            return view;
        }

        class Holder {
            TextView textView;
        }
    }

}
